package unq.tpi.persistencia.performanceEj.daos;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.classic.Session;

import unq.tpi.persistencia.performanceEj.model.Department;
import unq.tpi.persistencia.performanceEj.model.Employee;
import unq.tpi.persistencia.util.SessionManager;

public class DepartmentDAO {

	public Department getByName(final String name) {
		Session session = SessionManager.getSession();
		return (Department) session
				.createQuery("from Department where name = :name")
				.setParameter("name", name).uniqueResult();
	}
	
    public Set<Employee> getAllEmployees(String number){
	    Session session = SessionManager.getSession();
	    Query q = session.createQuery("from Department as d inner join fetch d.employees e inner join fetch e.salaries where d.number = :number");
	    q.setParameter("number", number);
	    Department d = (Department) q.uniqueResult();

	    return d.getEmployees();	
	    }
	
	public Department getByCode(String num) {
		Session session = SessionManager.getSession();
		return (Department) session.get(Department.class, num);
	}

    @SuppressWarnings("unchecked")
    public List<Department> getAll() {
		Session session = SessionManager.getSession();
		//return session.createCriteria(Department.class).setFetchMode("managers", FetchMode.JOIN).list();
		return session.createQuery("from Department as d inner join fetch d.managers").list();
    }

}
